package com.amcase.demk.filter;

import com.amcase.demk.base.Constance;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName:EncodingFilter
 * @Decription: 过滤请求处理请求及响应的编码
 * @Author:amcase
 * @Data:2018/4/11 16:10
 * @Version:1.0
 **/
@WebFilter("/*")
public class EncodingFilter implements Filter{


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest)servletRequest;
        HttpServletResponse httpResponse = (HttpServletResponse)servletResponse;

        //设置统一编码格式
        httpRequest.setCharacterEncoding(Constance.PROJECT_ENCODING);
        httpResponse.setCharacterEncoding(Constance.PROJECT_ENCODING);

        filterChain.doFilter(httpRequest,httpResponse);
    }

    @Override
    public void destroy() {

    }
}
